package com.pay.ioopos.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aggregate.pay.sanstar.SanstarApiFactory;
import com.aggregate.pay.sanstar.bean.Result;
import com.aggregate.pay.sanstar.bean.TerminalInfoResult;
import com.aggregate.pay.sanstar.support.Client;
import com.aggregate.pay.sanstar.support.Merch;
import com.pay.ioopos.ApiFactory;
import com.pay.ioopos.BindState;
import com.pay.ioopos.R;
import com.pay.ioopos.Utils;


public class ShopInfoFragment extends AbstractNetworkFragment implements BindState {
    private View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return view = inflater.inflate(R.layout.fragment_shop_info, container, false);
    }

    @Override
    protected void execute() throws Exception {
        Merch merch = ApiFactory.initApi();

        Client<Void, TerminalInfoResult> client = SanstarApiFactory.terminalInfo(merch);

        Result<TerminalInfoResult> apiResult = client.execute(null);

        if (apiResult.getStatus() != Result.Status.OK) {
            onError("[" + apiResult.getCode() + "]" + apiResult.getMessage());
            return;
        }

        TerminalInfoResult info = apiResult.getData();
        handle(() -> {
            TextView merchName = view.findViewById(R.id.merch_name);
            merchName.setText(info.getMerchName());
            TextView merchNo = view.findViewById(R.id.merch_no);
            merchNo.setText(info.getMerchNo());
            TextView shopName = view.findViewById(R.id.shop_name);
            shopName.setText(info.getShopName());
            TextView shopSn = view.findViewById(R.id.shop_sn);
            shopSn.setText(String.valueOf(info.getShopSn()));
            TextView terminalSn = view.findViewById(R.id.terminal_sn);
            terminalSn.setText(String.valueOf(info.getTerminalSn()));
            TextView sn = view.findViewById(R.id.sn);
            sn.setText(Utils.sn());
        });
    }

}
